#include "sendfilewidget.h"
#include "ui_sendfilewidget.h"
#include <QDebug>
static QAction *del;
static QAction *open;
static QAction *openinpath;
static QAction *add;
static QAction *clear;
SendFileWidget::SendFileWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SendFileWidget)
{
    ui->setupUi(this);
    //设置为图标模式
    ui->listWidget->setViewMode(QListView::IconMode);
    ui->listWidget->setIconSize(QSize(60,60));
    ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    ui->listWidget->setWordWrap(true);
    this->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowCloseButtonHint);
    this->setAttribute(Qt::WA_DeleteOnClose);
}

SendFileWidget::~SendFileWidget()
{
    delete ui;
}

void SendFileWidget::selectFiles()
{
    QStringList tmp=QFileDialog::getOpenFileNames(this,"请选择你要发送的文件","C:","");
    path.append(tmp);
    for(QString p:tmp)
        addContent(p);
}

void SendFileWidget::removeFile(int row)
{
    if(row<0)return;
    QListWidgetItem* myitem= ui->listWidget->takeItem(row);
    path.removeAt(row);
    delete myitem;
}

void SendFileWidget::addContent(QString filename)
{
    QListWidgetItem *item=nullptr;
    QPixmap p2(filename);
    QFileInfo info=QFileInfo(filename);
    if(filename.endsWith("png")|filename.endsWith("jpg")|filename.endsWith("bmp")){

        item=new QListWidgetItem(p2.scaled(60,60),info.fileName());
    }
    else{
        item=new QListWidgetItem(QPixmap(":/icons/Src/Icons/ImgBoxIcon/file.png").scaled(60,60),info.fileName());
    }
    item->setSizeHint(QSize(121,123));
    ui->listWidget->addItem(item);

}


void SendFileWidget::on_listWidget_customContextMenuRequested(const QPoint &pos)
{

    QMenu menu(this);
    if(ui->listWidget->itemAt(pos)){
    open=menu.addAction("打开");
    del= menu.addAction("移除");
    openinpath=menu.addAction("在文件管理器中打开");
    ui->listWidget->itemAt(pos)->setSelected(true);
    connect(del,&QAction::triggered,this,[=](){
        removeFile(ui->listWidget->currentRow());
    });

    connect(open,&QAction::triggered,this,[=](){
        QDesktopServices::openUrl(path[ui->listWidget->currentRow()]);
    });
    connect(openinpath,&QAction::triggered,this,[=](){
        QString tmp=path[ui->listWidget->currentRow()];
        QFileInfo info(tmp);
        QDesktopServices::openUrl(info.path());
    });
    }
    else
    {
        add=menu.addAction("添加");
        clear=menu.addAction("清空");
        connect(add,&QAction::triggered,this,[=](){
            selectFiles();
        });
        connect(clear,&QAction::triggered,this,[=](){
            path.clear();
            ui->listWidget->clear();
        });
    }
    menu.exec(QCursor::pos());

}

void SendFileWidget::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    removeFile(ui->listWidget->currentRow());
}

void SendFileWidget::on_btn_cancel_clicked()
{
    this->close();
}

void SendFileWidget::on_btn_send_clicked()
{
    emit selectedFileDone(path);
    this->close();
}



void SendFileWidget::on_btn_addfile_clicked()
{
    selectFiles();
}

void SendFileWidget::on_btn_deletefile_clicked()
{
    removeFile(ui->listWidget->currentRow());
}

void SendFileWidget::on_btn_openindir_clicked()
{
    QDesktopServices::openUrl(path[ui->listWidget->currentRow()]);
}
